import {getCraneList, getLiftList, getCommonFvsList, getDustList, getGantryList, getBridgeList} from "@/api/api.js";
import {getcrawlerList} from "@/api/crawler";
import {initSceneTypeDataFn} from "@/sever/scene-type-list";

export const senceInit = {
    data() {
        return {
            craneList: [],
            liftList: [],
            fvsList: [],
            dustList: [],
            treeList: [],
        }
    },
    methods: {
        getInit() {
            this.treeList = [];
            // 请求参数params
            let params1 = {
                deptId: this.$store.state.porjectObject.deptId,
                bindFlag: 1,
            }
            // 请求参数params
            let params2 = {
                deptId: this.$store.state.porjectObject.deptId,
            };
            //回调函数
            let callBackList = [
                {
                    type:1,//特种设备-塔机
                    callback:getCraneList
                },
                {
                    type:1,//特种设备-升降机
                    callback:getLiftList
                },
                {
                    type:2, //环境检测仪
                    callback:getDustList
                },
                {
                    type:2,// 视频监控
                    callback:getCommonFvsList
                },
                {
                    type:1,// 龙门吊
                    callback:getGantryList
                },
                {
                    type:1,// 架桥机
                    callback:getBridgeList
                },
                {
                    type:1,// 履带吊
                    callback:getcrawlerList
                },
                // {
                //     type:2,// 智能水表
                //     callback:getwaterList
                // },
                // {
                //     type:2, // 智能水表
                //     callback:getelectricityList
                // },
                // {
                //     type:2,// 智能电箱
                //     callback:getEnergyElectricBoxList
                // },
            ];
            // 数组长度
            let length = callBackList.length;
            // 全部的 promise 请求体
            let promiseList = [];
            for (let i = 0; i < length; i++) {
                let callback = callBackList[i].callback;
                let params = callBackList[i].type == 1? params1 : params2
                let p = new Promise((resolve, reject) => {
                    callback(params).then(res => {
                        resolve(res)
                    }).catch(err => {
                        reject(err)
                    })
                });
                promiseList.push(p);
            }
            Promise.all(promiseList).then(res => {
                let craneList = res[0].data.list;
                let liftList = res[1].data.list;
                let dustList = res[2].data.list;
                let fvsList = res[3].data.list;
                let gantryList = res[4].data.list;
                let bridgeList = res[5].data.list;
                let crawlerList = res[6].data.list;
                let waterList = res[7].data.list;
                let electricityList = res[8].data.list;
                let boxList = res[9].data.list;

                let envArr = dustList.filter(v => v.deviceType == 5);
                let waterArr = dustList.filter(v => v.deviceType == 4);
                let gasArr = dustList.filter(v => v.deviceType == 12);

                waterArr.map(v => v['objType'] = 4);
                envArr.map(v => v['objType'] = 5);
                craneList.map(v => v['objType'] = 6);
                liftList.map(v => v['objType'] = 7);
                gantryList.map(v => v['objType'] = 8);
                bridgeList.map(v => v['objType'] = 9);
                crawlerList.map(v => v['objType'] = 10);
                gasArr.map(v => v['objType'] = 12);
                fvsList.map(v => v['objType'] = 20);
                waterList.map(v => v['objType'] = 30);
                electricityList.map(v => v['objType'] = 31);
                boxList.map(v => v['objType'] = 32);
                let list = [
                    ...waterArr,
                    ...envArr,
                    ...craneList,
                    ...liftList,
                    ...gantryList,
                    ...bridgeList,
                    ...crawlerList,
                    ...gasArr,
                    ...fvsList,
                    ...waterList,
                    ...electricityList,
                    ...boxList,
                ]
                this.treeList = initSceneTypeDataFn(list);
            })
        }
    },
}
